<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache,no-store,must-revalidate">
    <meta http-equiv="Cache" content="no-cache">
		<title>新增页面</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="">
			<div class="mainBox">
				<div class="main-container">

					<div class="layui-form-item">
						<label class="layui-form-label">部位名称</label>
						<div class="layui-input-block">
							<input type="text" name="partName" lay-verify="required" autocomplete="off" placeholder="请输入部位名称"
								class="layui-input">
						</div>
					</div>
					
					<div class="layui-form-item">
						<label class="layui-form-label">部位属于</label>
						<div class="layui-input-block">
							<input type="radio" name="belong2" value="belong2SecD" title="工程下所有设备" lay-filter="belong2-filter" checked>
							<input type="radio" name="belong2" value="belong2MacNo" title="设备号" lay-filter="belong2-filter" >
						</div>
					</div>
					
					<div class="layui-form-item">
						<label class="layui-form-label">所属工程</label>
						<div class="layui-input-block">
							<select name="belong2SecD" lay-verify="required" lay-filter="belong2SecD-filter">
								<option value=""></option>
							</select>
						</div>
					</div>

					<div class="layui-form-item" id="layui-form-item-belong2MacNo" style="display:none">
						<label class="layui-form-label">所属设备</label>
						<div class="layui-input-block">
							<select name="belong2MacNo" lay-verify="" lay-filter="belong2MacNo-filter">
								<option value=""></option>
							</select>
						</div>
					</div>

				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="partAdd">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>


		
		<script type="text/javascript">
			// 加载自定义模块,检查浏览器和token
			layui.extend({
        myheader: "{/}/lay-ext/myheader",
      });
			
			
			layui.use(['form', 'jquery','myheader','layer','laytpl'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let myheader = layui.myheader;
				let layer = layui.layer;
				let laytpl = layui.laytpl;

				var optionData={
					secdlist:[],
					macnolist:[]
				}

				// 定义全局 ajax的请求头
				myheader.ajaxGlobal(myheader.checkToken().tokenValue);

				getAllSecD();

				// 单选切换事件
				form.on('radio(belong2-filter)',function(data){
					if(data.value =="belong2SecD"){
						$('#layui-form-item-belong2MacNo').hide()
						$("[name=belong2MacNo]").attr("lay-verify","")
					} else if (data.value=="belong2MacNo"){
						$('#layui-form-item-belong2MacNo').show()
						$("[name=belong2MacNo]").attr("lay-verify","required")
					}
					//$("[name=belong2MacNo] option:selected").text("")
					// 清空 select machineNo 模板
					clearMacSelectTpl()
					// 获取并渲染secd
					getAllSecD();

				})

				// 所属工程选择框, 选择事件
				form.on('select(belong2SecD-filter)',function(data){
					// 单选选中 设备号时, ajax获取设备号list,并填充模板
					if($("input[name='belong2']:checked").val()==="belong2MacNo"){
						getAllMacNo(data.value);
					}
				})

				// form 确认上传
				form.on('submit(partAdd)', function(data) {
					$.ajax({
						url: parent.layui.myheader.baseUrl()+ '/part/addPart',
						data: JSON.stringify({
							partName:data.field.partName,
							secDeptID:data.field.belong2SecD,
							machineNo:data.field.belong2MacNo
						}),
						contentType: 'application/json',		// 请求时的数据类型
						type: 'post',
						success: function(res) {
							if (res.data==1) {
								layer.msg("已添加", {
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window
										.name)); //关闭当前页
									parent.layui.table.reloadData('part-table',{
										scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
									});
								});
							} else {
								layer.msg("添加失败", {
									time: 1000
								});
							}
						}
					})
					return false;
				});
			
				$("[type='reset']").click(function(){
					$('#layui-form-item-belong2MacNo').hide()
					clearMacSelectTpl()
					getAllSecD()
				})

				// 获取和填充工程 select
				function getAllSecD(){
					// 获取select 数据
					$.ajax(parent.layui.myheader.baseUrl()+"/secDept/getAllSecDeptName",{
						async:false, // 同步方法
						success: function(res){
							if(res.data.length>0){
								optionData.secdlist =[...res.data]
							} else {
								optionData.secdlist =[]
								parent.layer.msg('未获取有效数据')
							}
						}
					})
					
					// 填充layui模板
					let getTpl = optionTpl.innerHTML
					laytpl(getTpl).render(optionData.secdlist,function(html){
						$("[name='belong2SecD']").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
				
				// 获取和填充设备号 select 设备号
				function getAllMacNo(secDeptID){
					$.ajax(parent.layui.myheader.baseUrl()+"/machine/getMachineList4SecDept",{
						async:false,  //同步方法
						data: {
							secDeptID: secDeptID
						},
						success: function(res){
							if(res.data.length>0){
								optionData.macnolist =[...res.data]
							} else {
								optionData.macnolist=[]
								parent.layer.msg('未获取有效数据')
							}
						}
					})
					// 填充layui模板
					let getTpl = optionMacTpl.innerHTML
					laytpl(getTpl).render(optionData.macnolist,function(html){
						$("[name='belong2MacNo']").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
				// 清空select 设备号模板
				function clearMacSelectTpl(){
					// 填充layui模板
					let getTpl = optionMacTpl.innerHTML
					laytpl(getTpl).render([],function(html){
						$("[name='belong2MacNo']").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
			})
		</script>

		<script id="optionTpl" type="text/html">
			<option value=""></option>
			{{# layui.each(d,function(index,item){ }}
				<option value="{{item.secDeptID}}">{{item.secDeptName}} </option>
			{{# }); }}
		</script>

		<script id="optionMacTpl" type="text/html">
			<option value=""></option>
			{{# layui.each(d,function(index,item){ }}
				<option value="{{item.machineNo}}">{{item.machineNo}} </option>
			{{# }); }}
		</script>

	</body>
</html>
